package com.cdvtc.flower.web.servlet;

import com.cdvtc.flower.dao.ProductsDao;
import com.cdvtc.flower.dao.impl.ProductsDaoImpl;
import com.cdvtc.flower.model.Products;

import javax.servlet.*;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet(name = "ProductsModifyServlet", value = "/ProductsModifyServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*10, 	// 10 MB
        maxFileSize=1024*1024*10,      	// 单个文件大小限制10M
        maxRequestSize=1024*1024*50)   	// 总文件大小限制 5OM
public class ProductsModifyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  // 防止中文文件名乱码
        BigDecimal original_price;
        BigDecimal sale_price;

        String product_id = request.getParameter("product_id");
        String product_name = request.getParameter("product_name");
        String category_id = request.getParameter("category_id");
        String originalPriceStr = request.getParameter("original_price");
        String salePriceStr = request.getParameter("sale_price");
        String description = request.getParameter("description");
        String soldStr = request.getParameter("sold"); // 获取字符串类型的参数
        String merchant_id = request.getParameter("merchant_id");
        Boolean soldBool = Boolean.parseBoolean(soldStr); // 尝试将字符串转换为布尔值

        original_price = new BigDecimal(originalPriceStr);
        sale_price = new BigDecimal(salePriceStr);
        Products products = new Products();
//        Category category = new Category();
        products.setProduct_id(Integer.parseInt(product_id));
        products.setProduct_name(product_name);
        products.setCategory_id(Integer.parseInt(category_id));
        products.setOriginal_price(original_price);
        products.setSale_price(sale_price);
        products.setDescription(description);
        products.setSold(soldBool);
        products.setMerchant_id(Integer.parseInt(merchant_id));


        // 获取图片
        Part part = request.getPart("image");
        if (part != null && part.getSize() > 0) {
            String uploadFilePath = this.getServletContext().getRealPath("tool/html_css_js_image_images/images/product");
            String cd = part.getHeader("Content-Disposition");
//截取不同类型的文件需要自行判断
            String fileName = cd.substring(cd.lastIndexOf("=")+2, cd.length()-1);
            // 写文件
            part.write(uploadFilePath + File.separator + fileName);
            products.setImage(fileName);
        }
        //保存信息至数据库
        ProductsDao productsDao = new ProductsDaoImpl();
        productsDao.updateProduct(products);
        //更新当前会话中用的户信息对象
        request.setAttribute("products", products);


        // 页面跳转
        request.setAttribute("page", "productsList.jsp");
        request.setAttribute("message", "商品信息修改成功。");
        request.getRequestDispatcher("alert_jump.jsp").forward(request, response);
    }
    }
